/*
 * Its an sample program to communicate with any server has telnet capabilities
 * Programmed for testing purpose. Available under GPL
 */

/*
 * GUITelnet.java
 *
 * Created on 12 Nov, 2010, 6:32:24 AM
 */

package org.antany.samples;

import java.awt.Toolkit;
import java.net.Socket;

/**
 *
 * @author antany
 */
public class GUITelnet extends javax.swing.JFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** Creates new form GUITelnet */
    public GUITelnet() {
        initComponents();
    }

    Socket myClient;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        lblIPAddress1 = new javax.swing.JLabel();
        lblPortNo = new javax.swing.JLabel();
        txtIPAddress = new javax.swing.JTextField();
        txtPortNo = new javax.swing.JTextField();
        btnCoinnect = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        lblStatus = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        btnDisconnect = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        btnSend = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        lblIPAddress1.setText("IP Address :");
        getContentPane().add(lblIPAddress1);
        lblIPAddress1.setBounds(20, 30, 90, 17);

        lblPortNo.setText("Port No      :");
        getContentPane().add(lblPortNo);
        lblPortNo.setBounds(20, 80, 90, 17);

        txtIPAddress.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIPAddressActionPerformed(evt);
            }
        });
        getContentPane().add(txtIPAddress);
        txtIPAddress.setBounds(110, 30, 300, 27);
        getContentPane().add(txtPortNo);
        txtPortNo.setBounds(110, 70, 300, 27);

        btnCoinnect.setText("Connect");
        btnCoinnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCoinnectActionPerformed(evt);
            }
        });
        getContentPane().add(btnCoinnect);
        btnCoinnect.setBounds(430, 30, 130, 29);
        getContentPane().add(jSeparator1);
        jSeparator1.setBounds(0, 410, 630, 10);

        lblStatus.setText("Status  : Not Connected");
        getContentPane().add(lblStatus);
        lblStatus.setBounds(20, 420, 200, 17);
        getContentPane().add(jSeparator2);
        jSeparator2.setBounds(0, 120, 630, 10);

        btnDisconnect.setText("Disconnect");
        btnDisconnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDisconnectActionPerformed(evt);
            }
        });
        getContentPane().add(btnDisconnect);
        btnDisconnect.setBounds(430, 70, 130, 29);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(10, 140, 490, 260);

        btnSend.setText("Send");
        btnSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendActionPerformed(evt);
            }
        });
        getContentPane().add(btnSend);
        btnSend.setBounds(510, 370, 110, 29);

        pack();
    }// </editor-fold>                        

    private void btnCoinnectActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        try{
            myClient = new Socket(txtIPAddress.getText(), Integer.valueOf(txtPortNo.getText()));
            System.out.println(myClient.getInetAddress());
           // myClient.getOutputStream().
        }catch(Exception e){
            e.printStackTrace();
        }
    }                                           

    private void btnDisconnectActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
    }                                             

    private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                       

    private void txtIPAddressActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
    }                                            

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                GUITelnet guiTelnet = new GUITelnet();
                guiTelnet.setVisible(true);
                Toolkit tk = Toolkit.getDefaultToolkit();
                int xSize = ((int) tk.getScreenSize().getWidth());
                int ySize = ((int) tk.getScreenSize().getHeight());
                guiTelnet.setBounds((xSize/2-320), (ySize/2-235), 640, 470);

            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnCoinnect;
    private javax.swing.JButton btnDisconnect;
    private javax.swing.JButton btnSend;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel lblIPAddress1;
    private javax.swing.JLabel lblPortNo;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JTextField txtIPAddress;
    private javax.swing.JTextField txtPortNo;
    // End of variables declaration                   

}
